<!--
 * @Description: 邀请pk赛活动管理列表
 * @Author: liyujie
 * @Date: 2021/2/23
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/2/23
-->
<template>
    <PageLayout
        title="邀请PK赛"
        class="invite-pk-activity-list-wrapper"
    >
        <!-- 筛选-->
        <template slot="filter">
            <zd-tabs
                :tab-list="tabsList"
                v-model="filterData.task_category"
                style="padding-left: 8px"
                @change="handleTabChange"
            />
            <div
                class="back"
                @click="handleRoute('inviteManage')"
            >返回</div>
            <div class="filter-view">
                <div class="filter-item">
                    <div class="filter-item__title">
                        活动信息
                    </div>
                    <el-input
                        placeholder="搜索活动编号"
                        v-model="filterData.task_no"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 223px"
                        @change="getTableData(1)"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="filter-item">
                    <div class="filter-item__title">
                        活动期数
                    </div>
                    <el-input
                        placeholder="搜索活动期数"
                        v-model="filterData.activity_index"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 223px"
                        @change="getTableData(1)"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="filter-item">
                    <div class="filter-item__title">
                        活动进度
                    </div>
                    <el-select
                        v-model="filterData.status"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="filter-item">
                    <el-button
                        type="primary"
                        size="small"
                        class="query-button"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                    <el-button
                        size="medium"
                        type="primary"
                        icon="el-icon-plus"
                        @click="handleRoute('invitePkActivityEdit', {type: 'add', task_category: filterData.task_category})"
                    >
                        创建活动
                    </el-button>
                </div>
            </div>
        </template>
        <!--筛选-->
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
            />
            <el-table-column
                prop="task_no"
                label="活动编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="activity_index"
                label="活动期数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    第{{ scope.row.activity_index }}期
                </template>
            </el-table-column>
            <el-table-column
                label="活动时间"
                align="left"
                :formatter="emptyFormatter"
                min-width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }} -
                    {{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column
                label="活动进度"
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot',scope.row.status === 0 ? 'warning' : (scope.row.status === 1 ? 'success' : 'normal')]"/>
                        {{ scope.row.status | statusFilter }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="school_count"
                label="参与学校数"
                align="center"
                v-if="filterData.task_category === 105"
            />
            <el-table-column
                prop="register_count"
                label="注册人数"
                align="center"
            />
            <el-table-column
                prop="certified_count"
                label="已认证人数"
                align="center"
            />
            <el-table-column
                label="操作"
                align="left"
                width="270"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleSetStatus(scope.row)"
                        >
                            {{ scope.row.task_status ? '下架' : '上架' }}
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            v-if="scope.row.status !== -1"
                            @click="handleEdit(scope.row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute(scope.row.task_category === 104 ? 'invitePkActivityDetail' : 'invitePkActivitySchoolList', {
                                task_no: scope.row.task_no,
                                activity_index: scope.row.activity_index,
                                start_time: scope.row.start_time,
                                end_time: scope.row.end_time
                            })"
                        >
                            查看数据
                        </el-button>
                        <!--<el-button
                            type="text"
                            size="medium"
                            @click="handleRoute('elitePkActivityEdit', {task_no: scope.row.task_no, type: 'copy'})"
                        >
                            复制
                        </el-button>-->
                        <el-button
                            type="text"
                            size="medium"
                            :style="{color: '#F95B56'}"
                            @click="handleDelete(scope.row)"
                        >
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import {back} from '@/mixin/back';
import ZdTabs from "@/components/zd-tabs";

export default {
    name: 'InvitePkActivityList',
    mixins: [back],
    components: {ZdTabs},
    data() {
        return {
            filterData: {
                task_no: '',
                activity_index: '',
                status: 99,
                task_category: 104
            },
            tabsList: [
                {label: '个人PK赛', value: 104},
                {label: '学校PK赛', value: 105},
            ],
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {label: '全部', value: 99},
                {label: '未开始', value: 0},
                {label: '进行中', value: 1},
                {label: '已结束', value: -1},
            ],
        };
    },
    computed: {},
    watch: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '未开始';
                case 1:
                    return '进行中';
                case -1:
                    return '已结束';
                default:
                    return '--';
            }
        },
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '邀请好友'}, {title: '邀请管理', path: '/student/invite-manage'}, {title: '邀请PK赛'}]);
        this.authResult = this.$route.meta.authResult;
        this.filterData.task_category = this.$route.query.active ? Number(this.$route.query.active) : 104;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/task_stuagent_extra_student_invite_activity%5Cactivity%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 编辑
         * @param row
         * @returns {boolean}
         */
        handleEdit(row) {
            if (!this.authResult.auth_edit) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.handleRoute('invitePkActivityEdit', {
                task_no: row.task_no,
                type: 'edit',
                task_category: row.task_category
            });
        },

        /**
         * 删除
         * @param row
         * @returns {boolean}
         */
        handleDelete(row) {
            if (!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$zdConfirm('确定是否删除该邀请PK赛活动？', '温馨提示', {
                confirmButtonText: '确认删除',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/task_stuagent_extra_student_invite_activity%5Cactivity%5Cdelete', {task_no: row.task_no}, res => {
                    if (res.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: res.msg,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 上下架
         * @param row
         * @returns {boolean}
         */
        handleSetStatus(row) {
            if (!this.authResult.auth_set_status) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$post('/student/task_stuagent_extra_student_invite_activity%5Cactivity%5Cset_status', {
                task_no: row.task_no,
                task_status : row.task_status ? 0 : 1
            }, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * tab切换
         */
        handleTabChange() {
            this.getTableData(1);
            this.$nextTick(() => {
                this.$refs.table.doLayout();
            });
            this.$router.push({
                query: {
                    active: this.filterData.task_category
                }
            });
        },
    }
};
</script>

<style lang="less" scoped>
.invite-pk-activity-list-wrapper {
    /deep/ .content-wrapper {
        .header {
            display: none;
        }

        .filter-wrapper {
            display: initial !important;
            margin-top: 0 !important;
            padding: 0 !important;
            position: relative;
            .back {
                font-size: 14px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                cursor: pointer;
                position: absolute;
                top: 20px;
                right: 20px;
            }

            .filter-view {
                display: flex;
                padding: 0 24px;
                margin-top: 24px;

                .left {
                    display: flex;
                    flex-wrap: wrap;
                    flex: 1;
                }

                .right {
                    width: 260px;
                    flex-shrink: 0;
                }
            }
        }
    }
}
</style>
